package com.ywjg.serviceimp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.ywjg.dao.AppGoodDao;
import com.ywjg.dao.AppShopDao;
import com.ywjg.dao.GoodModelDao;
import com.ywjg.dao.GoodTypeDao;
import com.ywjg.model.AppGood;
import com.ywjg.model.AppShop;
import com.ywjg.model.GoodModel;
import com.ywjg.model.GoodType;
import com.ywjg.service.IAppGoodService;
import com.ywjg.utils.ApplicationModel;
import com.ywjg.utils.Utils;
@Service
public class AppGoodService implements IAppGoodService  {
	
	@Resource
	AppGoodDao appGoodDao;
	@Resource
	GoodTypeDao goodTypeDao;
	@Resource
	AppShopDao appShopDao;
	@Resource
	GoodModelDao goodModelDao;
	@Autowired
    ApplicationModel applicationModel;


	
	public List<AppGood> findAllGood() {
		
		return appGoodDao.findByIsFixingAndGoodIsDelete(0, 0);
	}
	
	
	
	
	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IAppGoodService#findAllGood()
	 */
	public List<AppGood> findAllFixing() {
		
		return appGoodDao.findByIsFixingAndGoodIsDelete(1, 0);
	}
	
	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IAppGoodService#findGoodByGoodShopId(java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	public List<AppGood> findGoodByGoodShopId(Integer shopId, Integer page, Integer size) {
		Pageable Pageable = new PageRequest(page, size);
		Page<AppGood> findAppGoodByGoodShopId = appGoodDao.findAppGoodByGoodShopId(shopId,Pageable);
		long totalElements = findAppGoodByGoodShopId.getTotalElements();
		applicationModel.getMap().put("findGoodByGoodShopId", (int)totalElements);
		return findAppGoodByGoodShopId.getContent();
	}
	
	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IAppGoodService#findGoodByGoodId(java.lang.Integer)
	 */
	public AppGood findGoodByGoodId(Integer id) {
		return appGoodDao.findOne(id);
	}
    /* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IAppGoodService#addGood(com.ywjg.model.AppGood)
	 */
	@Transactional
	public AppGood addGood(AppGood g) {
		AppShop shop = appShopDao.findOne(g.getGoodShopId());
		g.setGoodShopName(shop.getShopName());
		String poInteger = Utils.getPoInteger(shop.getShopAddress());
		g.setGoodAddress(shop.getShopAddress());
		g.setGoodAddressPoi(poInteger);
		return appGoodDao.save(g);
	}
	
	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IAppGoodService#upDateAppGood(com.ywjg.model.AppGood)
	 */
	@Transactional
	public AppGood upDateAppGood(AppGood g) {
		AppGood appGood = appGoodDao.findOne(g.getId());
		Utils.copyPropertiesIgnoreNull(g, appGood);
		return appGoodDao.save(appGood);
	}
	
	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IAppGoodService#getFixingByType(java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	public List<AppGood> getFixingByType(Integer id,Integer page,Integer size) {
		Pageable pageable = new PageRequest(page, size);
		Page<AppGood> goods = appGoodDao.findAppGoodByGoodTypeIdAndIsFixing(id, 1, pageable);
        long totalElements = goods.getTotalElements();
		applicationModel.getMap().put("getFixingByType", (int)totalElements);
		return goods.getContent();
	}
	
	
	
	public List<AppGood> getFixingByTypeOderByPrice(Integer id,Integer page,Integer size) {
//		Pageable pageable = new PageRequest(page, size);
		Pageable x = new PageRequest(page, size, Direction.ASC, "thisModelPrice");
		 Page<GoodModel> allGoodModel = goodModelDao.findAll(x);
		 List<GoodModel> goodModels = allGoodModel.getContent();
		 List<AppGood> appGoods = new ArrayList<AppGood>();
		for(GoodModel gm:goodModels) {
			AppGood appGood = gm.getAppGood();
			if (null==appGood.getPrice()) {
				appGood.setPrice(gm.getThisModelPrice());
				appGoods.add(appGood);
			}
		}
        long totalElements = allGoodModel.getTotalElements();
		applicationModel.getMap().put("getFixingByType", (int)totalElements);
		return appGoods;
	}


	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IAppGoodService#findGoodNameLike(java.lang.String, int, int)
	 */
	public List<AppGood> findGoodNameLike(String name,int page,int size) {
		Pageable Pageable = new PageRequest(page, size);
		Page<AppGood> appGoods = appGoodDao.findByGoodNameLike("%"+name+"%",Pageable);
		long totalElements = appGoods.getTotalElements();
		applicationModel.getMap().put("findGoodNameLike", (int)totalElements);
		return appGoods.getContent();
	}

	
	 
	
	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IAppGoodService#getGoodTypeByParentId(int)
	 */
	public List<GoodType> getGoodTypeByParentId(int id) {
		
		return goodTypeDao.getGoodTypeByParentId(id);
	}



    /* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IAppGoodService#deleteAppGood(com.ywjg.model.AppGood)
	 */
	public void deleteAppGood(AppGood g) {
		// TODO Auto-generated method stub
		
	}
	
	

	
	

	
}